<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2022-11-22
 * Time: 18:42
 */

namespace App\controller\wechat;
use App\server\wechat\OfficialAccount;
use EasyWeChat\Kernel\Messages\Text;
use support\Request;
use support\exception\Forbidden;
use App\server\wechat\ResponseText;
class Server
{
    public function index(Request $request)
    {
        $o=new OfficialAccount($request->get('id'));
        $app=$o->getApp($request->get('id'));
        $config=$o->config;
        //$app=OfficialAccount::app($request->get('id'));
        $app->server->push(function ($message) use ($config){
            if($message['ToUserName']!==$config['user_name']){
                return '访问末授权公众号';
            }
            switch ($message['MsgType']) {
                case 'event':
                    return '收到事件消息1212';
                    break;
                case 'text':
                    $r=new ResponseText($message);
                    $h=$r->handle();

                    return $h;
                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                case 'file':
                    return '收到文件消息';
                // ... 其它消息
                default:
                    return '收到其它消息';
                    break;
            }
        } );
        $response = $app->server->serve();
        return response( $response->getContent(),200,['Content-Type' => 'text/xml']);
    }
}